package com.zzw.controller;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.zzw.aspect.AsyncService;
import com.zzw.bean.Msg;
import com.zzw.bean.TChargeBox;
import com.zzw.service.TChargeBoxService;
import com.zzw.utils.RedisUtil;
import com.zzw.utils.WebUtils;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;

/**
 * 充电盒控制器
 * @author 赵志伟
 * @version 1.0
 */
@SuppressWarnings({"all"})
@RequestMapping({"/tChargeBox"})
@Controller
@Api(tags = "充电盒管理接口")
public class TChargeBoxController extends BaseController {
    @Autowired
    private TChargeBoxService tChargeBoxService;
    @Autowired
    private AsyncService asyncService;

    @ApiOperation(value = "充电盒模板页面", notes = "跳转到充电盒模板列表页面")
    @RequestMapping("list_template")
    public String list_template() {
        return "tchargebox/tchargebox_template/list";
    }

    @ApiOperation(value = "充电盒下拉页面", notes = "跳转到充电盒下拉列表页面")
    @RequestMapping("list_drop")
    public String list_drop() {
        return "tchargebox/tchargebox_drop/list";
    }

    @ApiOperation(value = "充电盒日期页面", notes = "跳转到充电盒日期列表页面")
    @RequestMapping("list_date")
    public String list_date() {
        return "tchargebox/tchargebox_date/list";
    }

    @ApiOperation(value = "充电盒标签页面", notes = "跳转到充电盒标签列表页面")
    @RequestMapping("list_tab")
    public String list_tab() {
        return "tchargebox/tchargebox_tab/list";
    }

    @ApiOperation(value = "充电盒弹性页面", notes = "跳转到充电盒弹性列表页面")
    @RequestMapping("list_flex")
    public String list_flex() {
        return "tchargebox/tchargebox_flex/list";
    }

    @ApiOperation(value = "保存充电盒", notes = "保存新的充电盒信息")
    @ApiImplicitParam(name = "file", value = "充电盒图片文件", required = true, dataType = "MultipartFile", paramType = "form")
    @RequestMapping("save")
    @ResponseBody
    public Msg save(@RequestParam("file") MultipartFile file, TChargeBox tChargeBox) {
        tChargeBoxService.save(file, tChargeBox);
        return Msg.success();
    }

    @ApiOperation(value = "删除充电盒", notes = "根据ID删除充电盒")
    @ApiImplicitParam(name = "id", value = "充电盒ID", required = true, dataType = "Integer", paramType = "query")
    @RequestMapping("delete")
    @ResponseBody
    public Msg delete(Integer id) {
        tChargeBoxService.delete(id);
        return Msg.success();
    }

    @ApiOperation(value = "更新充电盒", notes = "更新充电盒信息")
    @ApiImplicitParam(name = "file", value = "充电盒图片文件", required = true, dataType = "MultipartFile", paramType = "form")
    @RequestMapping("update")
    @ResponseBody
    public Msg update(@RequestParam("file") MultipartFile file, TChargeBox tChargeBox) {
        tChargeBoxService.update(file, tChargeBox);
        return Msg.success();
    }

    @ApiOperation(value = "条件查询充电盒", notes = "根据条件分页查询充电盒数据")
    @ApiImplicitParams({
        @ApiImplicitParam(name = "pageNum", value = "当前页码", required = false, dataType = "Integer", paramType = "query", defaultValue = "1"),
        @ApiImplicitParam(name = "pageSize", value = "每页记录数", required = false, dataType = "Integer", paramType = "query", defaultValue = "10"),
        @ApiImplicitParam(name = "sort", value = "排序字段", required = false, dataType = "String", paramType = "query"),
        @ApiImplicitParam(name = "order", value = "排序方式", required = false, dataType = "String", paramType = "query")
    })
    @RequestMapping("/listByCondition")
    @ResponseBody
    public Msg listByCondition(@RequestParam(value = "pageNum", defaultValue = "1") Integer pageNum,
                               @RequestParam(value = "pageSize", defaultValue = "10") Integer pageSize,
                               @RequestParam(value = "sort", defaultValue = "") String sort,
                               @RequestParam(value = "order", defaultValue = "") String order,
                               TChargeBox tChargeBox) {

        PageHelper.startPage(pageNum, pageSize, WebUtils.getOrderBy(sort, order));

        List<TChargeBox> tChargeBoxes = tChargeBoxService.listByCondition(tChargeBox);
        PageInfo pageInfo = new PageInfo(tChargeBoxes, pageSize);

        //页脚数据
        //确保页脚数据是根据查询条件动态生成的，避免统计与分页数据不一致。
        List<TChargeBox> footer = tChargeBoxService.getFooterData(tChargeBox);

        return Msg.success()
                .add("pageInfo", pageInfo) //分页数据
                .add("footer", footer);    //页脚数据
    }

    @ApiOperation(value = "查询充电盒列表", notes = "查询充电盒列表数据")
    @ApiImplicitParams({
        @ApiImplicitParam(name = "pageNum", value = "当前页码", required = false, dataType = "Integer", paramType = "query", defaultValue = "1"),
        @ApiImplicitParam(name = "pageSize", value = "每页记录数", required = false, dataType = "Integer", paramType = "query", defaultValue = "10"),
        @ApiImplicitParam(name = "sort", value = "排序字段", required = false, dataType = "String", paramType = "query"),
        @ApiImplicitParam(name = "order", value = "排序方式", required = false, dataType = "String", paramType = "query")
    })
    @RequestMapping("/selectTchargeBoxList")
    @ResponseBody
    public Msg selectTchargeBoxList(@RequestParam(value = "pageNum", defaultValue = "1") Integer pageNum,
                               @RequestParam(value = "pageSize", defaultValue = "10") Integer pageSize,
                               @RequestParam(value = "sort", defaultValue = "") String sort,
                               @RequestParam(value = "order", defaultValue = "") String order,
                               TChargeBox tChargeBox) {

        PageHelper.startPage(pageNum, pageSize, WebUtils.getOrderBy(sort, order));

        List<TChargeBox> tChargeBoxes = tChargeBoxService.selectTchargeBoxList(tChargeBox);
        PageInfo pageInfo = new PageInfo(tChargeBoxes, pageSize);

        //页脚数据
        //确保页脚数据是根据查询条件动态生成的，避免统计与分页数据不一致。
        List<TChargeBox> footer = tChargeBoxService.getFooterData(tChargeBox);

        return Msg.success()
                .add("pageInfo", pageInfo) //分页数据
                .add("footer", footer);    //页脚数据
    }

    @ApiOperation(value = "导出充电盒数据", notes = "将充电盒数据导出为Excel")
    @RequestMapping("/export")
    public void exportExcel(TChargeBox tChargeBox,HttpServletRequest request, HttpServletResponse response) throws IOException {
        asyncService.excel(tChargeBox, request, response);
    }

    @ApiOperation(value = "测试异步处理", notes = "测试异步数据处理功能")
    @RequestMapping("test")
    public void test(HttpServletRequest request, HttpServletResponse response) throws IOException {
        asyncService.processData(response);
    }
}
